package org.gikolet.ch2.android.viewer;

import java.io.IOException;

import org.gikolet.ch2.android.R;
import org.gikolet.ch2.android.widget.ResListView;
import org.gikolet.ch2.data.ThreadHeader;
import org.gikolet.ch2.logic.AboneException;
import org.gikolet.ch2.logic.Ch2Exception;
import org.gikolet.ch2.logic.NotModifiedException;
import org.gikolet.ch2.logic.ResesLogic;

import roboguice.inject.ContextScoped;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.inject.Inject;

@ContextScoped
public class ResesViewer extends Ch2Viewer {

	private static final int MENU_RES_WRITE = 0;

	@Inject
	private ResListView resListView;

	@Inject
	private ResesLogic resesLogic;

	@Inject
	private Context context;

	private ThreadHeader threadHeader;

	public ResesViewer() {
	}

	@Override
	public void createView(ViewGroup parent) {

		parent.addView(this.resListView);
	}

	@Override
	public boolean createMenu(Menu menu) {
		// 板一覧更新メニューの追加
		MenuItem resWriteMenu = menu.add(0, MENU_RES_WRITE, 0,
				R.string.MENU_RES_WRITE);
		resWriteMenu.setIcon(android.R.drawable.ic_menu_edit);

		super.createMenu(menu);

		return true;
	}

	@Override
	public boolean onMenuItemSelected(MenuItem item) {
		if (super.onMenuItemSelected(item)) {
			return true;
		}

		switch (item.getItemId()) {

		case MENU_RES_WRITE:
			writeRes();
			return true;
		default:
			return false;
		}
	}

	private void writeRes() {
		ThreadHeader threadHeader = this.threadHeader;

		if (threadHeader != null) {
			new AlertDialog.Builder(this.context).setTitle("書込").setMessage(
					"未実装").setPositiveButton("OK", null).show();
		}
	}

	public void load(ThreadHeader threadHeader, boolean connect, boolean cache) {
		// if (!cache) {
		UpdateTask task = new UpdateTask();

		task.execute(threadHeader);

		// } else {
		// ResesViewer.this.resesAdapter.setSubject(subject);
		// }

		requestView();
	}

	class UpdateTask extends AsyncTask<ThreadHeader, Integer, Ch2Exception>
			implements OnCancelListener {

		private ThreadHeader threadHeader;

		ProgressDialog progressDialog;

		@Override
		protected Ch2Exception doInBackground(ThreadHeader... params) {

			try {
				this.threadHeader = params[0];

				resesLogic.update(params[0], true);

				return null;

			} catch (Ch2Exception e) {
				return e;

			} catch (IOException e) {
				throw new Ch2Exception(e);
			}
		}

		@Override
		protected void onPreExecute() {
			progressDialog = new ProgressDialog(context);

			progressDialog.setTitle("スレッド一覧更新中");
			progressDialog.setMessage("しばらくお待ちください");
			progressDialog.setIndeterminate(true);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setCancelable(true);
			progressDialog.setOnCancelListener(this);

			progressDialog.show();
		}

		public void onCancel(DialogInterface dialog) {
			cancel(true);
		}

		@Override
		protected void onPostExecute(Ch2Exception result) {
			if (result != null) {
				String errMsg;
				if (result instanceof NotModifiedException) {
					// TODO 文字列は外部に出そうぜ…
					// 更新無し
					errMsg = "更新がありませんでした";

				} else if (result instanceof AboneException) {
					// あぼ～ん
					errMsg = "あぼ～んを検出しました";
				} else {
					result.printStackTrace();
					errMsg = "原因不明のエラーです";
				}
				Toast.makeText(context, errMsg, 0).show();
			}
			ResesViewer.this.resListView.setThreadHeader(this.threadHeader);
			ResesViewer.this.resListView.setSelectionAfterHeaderView();
			progressDialog.dismiss();
		}
	}

	@Override
	public void setFocus() {
		this.resListView.requestFocus();
	}
}
